package com.carlinfo.demo.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.DbUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IAKingDao;
import com.carlinfo.demo.pojo.AKing;

public class AKingDaoImpl extends BaseDaoImpl<AKing> implements IAKingDao
{
	AKing king = new AKing();
	@Override
	public int saveOneDao(AKing king)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "insert into a_king value (null,?,?,?,?,?,?,?,?,?,?,?)";
		paramsList.add(king.getDynastyId());
		paramsList.add(king.getName());
		paramsList.add(king.getAge());
		paramsList.add(king.getHeight());
		paramsList.add(king.getMiaoHao());
		paramsList.add(king.getNianHao());
		paramsList.add(king.getContext());
		paramsList.add(king.getStatus());
		paramsList.add(king.getCreateTime());
		paramsList.add(king.getUpdateTime());
		paramsList.add(king.getPubTime());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int res =  this.executeJDBC(sql, paramsList, resultMap);
		king.setId(Integer.valueOf(resultMap.get("id")+""));
		return res;
	}

	@Override
	public int updateOneDao(AKing king)
	{
		List<Object> paramsList = new ArrayList<Object>();
		/**
		 * 修改的时候应该先查出来
		 */
		String sql = "update a_king set dynastyId = ? , name = ? , "
				+ "age = ? , height = ? ,miaoHao = ? , nianHao = ? , " + " context = ? , status = ? , "
				+ "createTime = ? , updateTime = ? , pubTime = ? " + "where id = ? ";
		paramsList.add(king.getDynastyId());
		paramsList.add(king.getName());
		paramsList.add(king.getAge());
		paramsList.add(king.getHeight());
		paramsList.add(king.getMiaoHao());
		paramsList.add(king.getNianHao());
		paramsList.add(king.getContext());
		paramsList.add(king.getStatus());
		paramsList.add(king.getCreateTime());
		paramsList.add(king.getUpdateTime());
		paramsList.add(king.getPubTime());
		paramsList.add(king.getId());
		this.pojoObj = king;
		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "delete from a_king ";
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			sql = sql + "where id = ?";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			sql = sql + "where name = ?";
			paramsList.add(condMap.get("name"));
		}
		this.pojoObj = king;
		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public AKing selectOneDao(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "select * from a_king ";
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			sql = sql + "where id = ?";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			sql = sql + "where name = ?";
			paramsList.add(condMap.get("name"));
		}
		this.pojoObj = king;
		return this.findOneJDBC(sql, paramsList);
	}

	/*protected AKing converFromDb(ResultSet res) throws SQLException
	{
		int id = res.getInt("id");
		int dynastyId = res.getInt("dynastyId");
		String name = res.getString("name");
		int age = res.getInt("age");
		double height = res.getDouble("height");
		String miaoHao = res.getString("miaoHao");
		String nianHao = res.getString("nianHao");
		String context = res.getString("context");
		byte status = res.getByte("status");
		Date createTime = res.getTimestamp("createTime");
		Date updateTime = res.getTimestamp("updateTime");
		Date pubTime = res.getTimestamp("pubTime");
		
		 * 不能打印,要返回 new出来的属性全部是空的; 将数据库取出来的值,一一赋值给属性
		 
		AKing king = new AKing();
		king.setId(id);
		king.setDynastyId(dynastyId);
		king.setName(name);
		king.setAge(age);
		king.setHeight(height);
		king.setMiaoHao(miaoHao);
		king.setNianHao(nianHao);
		king.setContext(context);
		king.setStatus(status);
		king.setCreateTime(createTime);
		king.setUpdateTime(updateTime);
		king.setPubTime(pubTime);
		return king;
	}*/

	@Override
	public List<AKing> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		List<AKing> kingList = new ArrayList<AKing>();
		String sql = "select * from a_king where 1=1 ";
		if (!"".equalsIgnoreCase(condMap.get("keyword") + "") && condMap.get("keyword") != null)
		{
			sql += " and ( name like ? or miaoHao like ? or nianHao like ? ) ";
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			paramsList.add(condMap.get("status"));
		}
		if (condMap.get("stDate") instanceof Date && condMap.get("enDate") instanceof Date)
		{
			sql += " and (pubTime >= ? and pubTime <= ? )";
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("enDate"));
		}
		/* 排序 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc, name desc";
		} else
		{
			/* 默认的效果 */
			sql += " order by pubTime desc";
		}
		this.pojoObj = king;
		if(pageInfoUtil != null)
		{
			return this.findListByPage(pageInfoUtil, sql, paramsList);
		}
		return this.findList(sql, paramsList);
	}

}
